Question link:Pku3259 wormholes
I read Dijkstra and floyed in the data structure book (I have never written it myself)
However, if there is a negative weight edge, it will never
So I found a specific question to see how to write it.
It seems that the key steps for bellmanford and Dijkstra to solve the problem are similar.
Are constantly relaxed
But the order of bellmanford is different.
Dijkstra is used to relax each vertex.
Bellmanford is the relaxation of each edge (actually a vertex of
POJ 3259 wormholes[★★☆☆☆] graph theory Shortest way bellman
Main topic:The essence is to find out whether the graph has a negative ring. That is, how to find out whether a graph contains a negative ring.It is important to note that the path in the input is bidirectional and wormhole is one-way
Sample ExampleInput:23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8Output:NOYES
Problem Solving Ideas:This problem is good, my optimization of a
POJ 3259 wormholesDescriptionWhile exploring he many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it's a one-way path that delivers the IT destination at a time that's before you entered the wormhole! Each of the FJ ' s farms comprises N (1≤n≤500) fields conveniently numbered 1..N, M (1≤m≤2500) paths, and W (1≤w≤200 ) wormholes.As FJ is a avid time-traveling fan, he wants to does the following:st
Link: http://poj.org/problem? Id = 3259 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 22010 # Problem/B
Wormholes
Time limit:2000 ms
Memory limit:65536 K
Total submissions:25079
Accepted:8946
DescriptionWhile processing his own farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-w
Amount The key is to read the question. I didn't know what it meant until I read the title report anyway. Give you n points. M route. Two-way. Time consuming. W Worm hole. One-way. The time is negative. Ask if you can return from a certain point of view before you see yourself. That is to judge whether there is negative ring. Using the Bellman_ford algorithm.Minutes to finish. A long line of bugs. Or the loop there J and I silly points unclear.Attached code:#include #include #include #include #d
respectively:
N,
M, And
W
Lines 2 ..
M+ 1 of each farm: three space-separated numbers (
S,
E,
T) That describe, respectively: a bidirectional path
SAnd
EThat requires
TSeconds to traverse. Two fields might be connected by more than one path.
Lines
M+ 2 ..
M+
W+ 1 of each farm: three space-separated numbers (
S,
E,
T) That describe, respectively: a one way path from
STo
EThat also moves the traveler back
TSeconds.
OutputLines 1 ..
F: For each farm, output "yes" if FJ can achieve his goal, otherw
(); to Q.pop (); +Inq[u] =0; - for(inti =0; I ){ the intv =E[u][i].first; * intval =E[u][i].second; $ if(D[v] > D[u] +val) {Panax NotoginsengD[V] = D[u] +Val; - if(Inq[v] = =0){ thecnt[v]++; +INQ[V] =1; A if(Cnt[v] >= N) {//determine a bit more than the total number of vertices, there is a negative ring theprintf"yes\n"); + return; - } $ Q.push (v); $ } -
Link: poj 3259
A famer has some farms, some of which contain some fields, some wormhole holes in the field, and a path (two-way) between the field and the field ), that is, the time from A to B and from B to a is C. the nature of wormhole: Time backflow. That is, the time spent from A to B through the wormhole is-C (one-way). I asked if he could go back to the starting point through the nature of the wormhole.
Idea: This question is actually to determ
integers respectively:
N,
M, And
WLines 2 ..
M+ 1 of each farm: three space-separated numbers (
S,
E,
T) That describe, respectively: a bidirectional path
SAnd
EThat requires
TSeconds to traverse. Two fields might be connected by more than one path. Lines
M+ 2 ..
M+
W+ 1 of each farm: three space-separated numbers (
S,
E,
T) That describe, respectively: a one way path from
STo
EThat also moves the traveler back
TSeconds. outputlines 1 ..
F: For each farm, output "yes" if FJ can achieve his goal
HDU 3259 Wormholes
Meaning: Give you an n, m, and t n representing the number of points, and m representing the number of two-way edges t representing the wormhole, now I want you to determine whether you can traverse the past.
Wormhole means that the side is one-way and negative weight (the input is a positive number)
Idea: Can I traverse the past point, that is, whether there is a negative ring, and apply the template decisively? The dijkstra alg
Wormholes (poj 3259 SPFA | Bellman_Ford negative ring), wormholespoj
Language:DefaultWormholes
Time Limit:2000 MS
Memory Limit:65536 K
Total Submissions:33291
Accepted:12130
DescriptionWhile processing his own farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to
Link: Click to open linkThe main idea:The subject test instructions to see most of the day is also not understand-_-| | |, and then go to poj the original topic discussion area to see, finally understand the topic. Test instructions is: Enter an F first, the Representative has the F Group test data, each group of test data entered first N, M, W, indicating that there are n points, M no edge, W has a forward edge, and that the weighted value of the edge should be negative (the input is a positive
Poj 3259 Wormholes (BELLman-FOrd algorithm) (Adjacent matrix representation)
I didn't do it at the beginning. I put it on hold for several days and saw the bug. So today, a drops and the code is pasted out. This is represented by an adjacent matrix. The next article uses an adjacent table to Improve the efficiency.
#include
#include
#include
#include
using namespace std;const int INF=600;int G[INF][INF];int inq[INF];int re
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.